import axios from 'axios'
import { useUserStore } from '@/stores'
import { showFailToast } from 'vant'

// 创建实例时配置默认值
const service = axios.create({
  baseURL: 'http://localhost:9999/',
})

// 请求拦截器
service.interceptors.request.use(
  async (config) => {
    const store = useUserStore()
    if (store.token) {
      config.headers.Authorization = store.token
    }
    return config
  },
  (error) => {
    return Promise.error(error)
  },
)

// 响应拦截器
service.interceptors.response.use(
  (res) => {
    return res.data
  },
  (error) => {
    return Promise.reject(error)
  },
)

export default service
